/*
 * Ex:			
 * File name:   test.cpp
 *
 *  Created on: Apr 6, 2012
 *      Author: ayash
 *
 *
 * Descriptor:
 *
 */

#include <cstdlib>
#include <string>
#include <iostream>
#include <pthread.h>


#include "outputdevice.h"
#include "common_defs.h"

using namespace std;

#define NUM_THREADS 2

uint num_writes_per_thread;

pthread_t thread[NUM_THREADS];

void* thread1(void* args)
{
	int res;
	string str("thread1: this is my first line\n");

	for ( int i = 0; i < num_writes_per_thread; i++ )
	{
		//res = write2device( (char *)str.c_str(), str.length() );

		//cout << "thread1:  res = " << res << endl;

		cout << "thread1: " << write2device( (char *)str.c_str(), str.length() ) << endl;

	}

	return NULL;
}

void* thread2(void* args)
{
	int res;

	string str("thread2: this is my first line\n");
	for ( int i = 0; i < num_writes_per_thread; i++ )
	{
		//res = write2device( (char *)str.c_str(), str.length() );

		//cout << "thread2: res = " << res << endl;
		cout << "thread2: " << write2device( (char *)str.c_str(), str.length() ) << endl;
	}


	return NULL;
}



int main(int argc, char **argv)
{
	int res;

	if ( 3 != argc )
	{
		cerr << "Usage: " << argv[0] << " <path of output file> <num writes per thread>"  << endl;

		return EXIT_FAILURE;
	}

	num_writes_per_thread = atoi(argv[2]);

	res = initdevice( argv[1] );

	pthread_create( & thread[0], NULL, thread1, NULL );
	pthread_create( & thread[1], NULL, thread2, NULL );

	for ( uint i = 0; i < NUM_THREADS; ++i )
	{
		 pthread_join( thread[i] ,NULL);
	}

	closedevice();
	
	usleep(200000);

	return res;
}


